package com.huanqiu.mylib.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.huanqiu.mylib.R;

/**
 * https://www.jianshu.com/p/705a6cb6bfee
 */
public class SettingMenuView extends RelativeLayout {

    private String str_text_right;

    private TextView text_left;
    private TextView text_right;
    private ImageView img_right;

    public SettingMenuView(Context context) {
        super(context);
        initView(context);
        showView();
    }

    public SettingMenuView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initAttrs(context, attrs);
        showView();
    }

    public SettingMenuView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initAttrs(context, attrs);
        showView();
    }

    public SettingMenuView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
        initAttrs(context, attrs);
        showView();
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_setting_menu, this, true);
        text_left = (TextView) findViewById(R.id.text_left);
        text_right = (TextView) findViewById(R.id.text_right);
        img_right = (ImageView) findViewById(R.id.img_right);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingMenuView);
        // 获取 left_text / right_text 属性
        String left_text = mTypedArray.getString(R.styleable.SettingMenuView_left_text);
        String right_text = mTypedArray.getString(R.styleable.SettingMenuView_right_text);
        mTypedArray.recycle();

        text_left.setText(left_text);
        text_right.setText(right_text);

        str_text_right = right_text;
    }

    private void showView() {
        if (">".equals(str_text_right)) {
            img_right.setVisibility(VISIBLE);
        } else {
            text_right.setVisibility(VISIBLE);
        }
    }

    public void setRightText(String text) {
        text_right.setText(text);
        text_right.setVisibility(VISIBLE);
        img_right.setVisibility(INVISIBLE);
    }

}
